
package Scene;

import java.awt.Dimension;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;


/**
 *
 * @author henriquediomede
 */
public class Camera
{
    private void initCamera()
    {
        position[0] = 0;
        position[1] = 10;
        position[2] = 0;
    }
    
    public Camera()
    {
        directions = new float[3];
        look = new float[3];
        position = new float[3];
        initCamera();
        fbMove = 0;
        lrMove = 0;
        viewPortSize = new Dimension();
    }
    
    public Camera(int x, int y, int z)
    {
        
    }
    
    public void reshapeViewPort(int width, int height)
    {
        viewPortSize.setSize(width, height);
    }
    
    public void placeCamera(GLAutoDrawable drawable)
    {
        GL gl = drawable.getGL();
        GLU glu = new GLU();
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
        glu.gluLookAt(position[0] , position[1], position[2], look[0], look[1], look[2], 0, 1, 0);
    }
    
    public void update()
    {
        final int temp [] = InputControl.getInput();
        
        if(temp[0] == 1)
            lrMove += 1;
        
        if(temp[1] == 1)
            lrMove -= 1;
        
        if(temp[2] == 1)
            fbMove += 1;
            
        if(temp[3] == 1)
            fbMove -=1;
        
       if(begin)
       {
           oldMouseHorizontal = InputControl.mouseHorizontalPosition();
           begin = false;
       }
       
        if((InputControl.mouseHorizontalPosition() - oldMouseHorizontal) < 0)
            horizontalAngle -= 4;
        else if(InputControl.mouseHorizontalPosition() - oldMouseHorizontal > 0)
            horizontalAngle += 4;
        
        
        verticalAngle = (float) ((viewPortSize.getHeight()/2 - InputControl.mouseVerticalPosition()) * VerticalAngleSpeed);
        
        oldMouseHorizontal = InputControl.mouseHorizontalPosition();
        
        directions[0] = (float) (Math.cos(Math.toRadians(horizontalAngle)));
        directions[1] = 0;
        directions[2] = (float) (Math.sin(Math.toRadians(horizontalAngle)));
       
        position[0] += fbMove * directions[0];
        position[1] += fbMove * directions[1];
        position[2] += fbMove * directions[2];
        
        position[0] += -lrMove * directions[2];
        position[1] += lrMove* directions[1];
        position[2] += lrMove * directions[0];
        
        
        look[0] = position[0] + directions[0];
        look[1] = position[1] + directions[1] + verticalAngle;
        look[2] = position[2] + directions[2]; 
        
        fbMove = 0;
        lrMove = 0;
    }
    
  
    private int fbMove;
    private int lrMove;
    private float look[];
    private float position[];
    private float directions[];
    private int horizontalAngle = 0;
    private float verticalAngle = 0.0f;
    private int oldMouseHorizontal;
    private boolean begin = true;
    private float VerticalAngleSpeed = 0.01f;
    private Dimension viewPortSize;
}
